package com.haixiaoke.saas.repository.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.repository.mapper.ApartmentMapper;
import com.haixiaoke.saas.repository.pojo.ApartmentCountInfo;
import com.haixiaoke.saas.repository.pojo.House;
import com.haixiaoke.saas.repository.pojo.HousingAllocation;
import com.haixiaoke.saas.repository.service.IApartmentService;
import com.haixiaoke.saas.repository.service.ICommunityBuildService;
import com.haixiaoke.saas.repository.service.IHouseService;
import com.haixiaoke.saas.repository.service.IHousingAllocationService;
import com.haixiaoke.saas.repository.vo.ApartmentDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 公寓Service
 */
@Service
public class ApartmentServiceImpl implements IApartmentService {

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IHousingAllocationService housingAllocationService;

    @Autowired
    private ICommunityBuildService communityBuildService;

    @Autowired
    private ApartmentMapper apartmentMapper;
    @Override
    public ApartmentDetailVo getDetail(String houseId) {
        House houseBaseInfo = houseService.selectHouseById(houseId);
        if (ObjectUtil.isEmpty(houseBaseInfo)) {
            return null;
        }
        HousingAllocation housingAllocation = housingAllocationService.selectHousingAllocationByHouseId(houseId);
        if (ObjectUtil.isNotEmpty(housingAllocation)) {
            // 拼装数据
            // title = 小区名 + 户型 eg: 胜利小区三室两厅
            String communityName = houseBaseInfo.getECommunityName();
            String houseType = "";
            Long room = houseBaseInfo.getRoom();
            Long hall = houseBaseInfo.getHall();
            Long toilet = houseBaseInfo.getToilet();
            // 数字转中文
            String roomStr = Convert.numberToChinese(room, false);
            String hallStr = Convert.numberToChinese(hall, false);
            String toiletStr = Convert.numberToChinese(toilet, false);
            if (StrUtil.isNotBlank(roomStr) && room != 0) {
                houseType += roomStr + "室";
            }
            if (StrUtil.isNotBlank(hallStr) && hall != 0) {
                houseType += hallStr + "厅";
            }
            if (StrUtil.isNotBlank(toiletStr) && toilet != 0) {
                houseType += toiletStr + "卫";
            }
            // 房型
            housingAllocation.setHouseType(houseType);
            // 标题
            String title = communityName + houseType;
            housingAllocation.setTitle(title);
            // 面积
            BigDecimal area = houseBaseInfo.getArea();
            housingAllocation.setArea(area);

            // 当前楼层
            Long floor = houseBaseInfo.getFloor();
            housingAllocation.setFloor(floor);

            // 总楼层
            Long buildId = houseBaseInfo.getBuildingNum();
            Long totalFloor = communityBuildService.getTotalFloor(buildId);
            housingAllocation.setTotalFloor(totalFloor);

            // 地址 address= 市 + 街道 + 小区名
            String cityName = houseBaseInfo.getCityName();
            String address = cityName + houseBaseInfo.getResidenceStreetName() + communityName;
            housingAllocation.setAddress(address);

            // 小区名
            housingAllocation.setCommunityName(communityName);

            String rentDetail = "";
            if (housingAllocation.getRentStartingTime() != null && housingAllocation.getRentStartingTime() != 0) {
                rentDetail += "起租时间" + housingAllocation.getRentStartingTime() + "天 ";
            }
            if (housingAllocation.getRentableStartTime() != null && housingAllocation.getRentableEndTime() != null) {
                rentDetail += "可租时间" + DateUtil.format(housingAllocation.getRentableStartTime(), "yyyy.MM.dd") + "-" + DateUtil.format(housingAllocation.getRentableEndTime(), "yyyy.MM.dd");
            }
            housingAllocation.setRentDetailTime(rentDetail);
        }

        ApartmentDetailVo vo = ApartmentDetailVo.builder().houseId(houseId).houseBaseInfo(houseBaseInfo).housingAllocation(housingAllocation).build();
        return vo;
    }

    /**
     * 获取同小区房源
     *
     * @param houseId
     * @return
     */
    @Override
    public List<ApartmentDetailVo> getHousingInSameCommunityByHouseId(String houseId) {
        House base = houseService.selectHouseById(houseId);
        String residenceCommunityId = base.getResidenceCommunityId();
        // 查询时需要排除自己
        List<String> ids = houseService.getHousingInSameCommunity(residenceCommunityId, houseId);
        List<ApartmentDetailVo> list = new ArrayList<>();
        for (String id : ids) {
            ApartmentDetailVo detail = getDetail(id);
            list.add(detail);
        }
        return list;
    }

    @Override
    public ApartmentCountInfo countApartment(ApartmentCountInfo info) {
        Integer totalCount = 0;
        List<Map<String, Long>> apartmentCountInfoList = apartmentMapper.getApartmentCountInfo(info);
        for (Map<String, Long> map : apartmentCountInfoList) {
            Object apartmentStateObj = map.get("apartmentState");
            Object countObj = map.get("count");

            Integer apartmentState = Convert.toInt(apartmentStateObj);
            Integer count = Convert.toInt(countObj);
            if (ObjectUtil.isNotNull(apartmentState)){
                switch (apartmentState){
                    //上架状态：0待上架 1上架 2下架
                    case 0:
                        info.setAwaitListing(count);
                        break;
                    case 1:
                        info.setAlreadyListed(count);
                        break;
                    case 2:
                        info.setNotAwaitListing(count);
                        break;
                }
            }
            totalCount += count;
        }
        info.setTotalCount(totalCount);
        return info;
    }
}
